<?php

class DB_Functions {

    private $db;

    
    function __construct() {
        include_once 'db_connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }

    
    function __destruct() {
        
    }

    

    /**
     * Getting all users
     */
    public function getAllUsers() {
       // $result = mysql_query("select * FROM gcm_users");
        return $result;
    }

    
     public function checkUserExisted($username) {
        $result = mysql_query("SELECT username from account WHERE username = '$username'");
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            // user existed
            return true;
        } else {
            // user not existed
            return false;
        }
    }
    
    // this method used for check login
    public function checkAvaibleAcount($username,$password){
    	$result = mysql_query("SELECT password FROM account WHERE username = '$username' LIMIT 1");
        $ret= mysql_fetch_array($result);
        
        if(strcmp($password,$ret[0])==0)  return true;
		return false;
        
    }
    
    // get gcm_regID for desktop device
    public function getRegIDFromUser($username){
    	$result = mysql_query("SELECT gcm_regid FROM account WHERE username = '$username' LIMIT 1");
        $ret= mysql_fetch_array($result);

		return  $ret[0];
        
    }
    
    public function updateGCMRegID($username,$regID){
    	
    	$result = mysql_query("UPDATE account SET gcm_regid ='$regID' WHERE username = '$username'");
    	 if ($result) {
            return true;
        } else {
            return false;
        }
    	
    }
    
    public function registerNew($username,$password, $gcm_regid,$email,$name ) {
        // insert user into database
        $result = mysql_query("INSERT INTO account(name, email, gcm_regid, created_at, username, password) VALUES('$name', '$email', '$gcm_regid', NOW() , '$username' , '$password') ");
        // check for successful store
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

}

?>
